package com.yinhe.overtimecalc;

enum TYPE {
    /**
     * 正数
     */
    POSITIVE,
    /**
     * 负数
     */
    NEGATIVE
}

public class OverTime {

    private static final int SEXAGESIMAL = 60;
    private int hour = 0;
    private int minute = 0;
    private TYPE type = TYPE.POSITIVE;

    public OverTime() {
    }

    public OverTime(int hour, int minute) {
        this.hour = hour;
        this.minute = minute;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        minute = carryBit(minute);
        this.minute = minute;
    }

    /**
     * 当minute大于SEXAGESIMAL时，自动进位给当前对象hour
     */
    private int carryBit(int minute) {
        if (minute >= SEXAGESIMAL) {
            hour = hour + minute / SEXAGESIMAL;
            minute = minute % SEXAGESIMAL;
        } else if (minute <= -SEXAGESIMAL) {
            hour = hour - 1;
            minute = SEXAGESIMAL + minute;
        }
        return minute;
    }

    public OverTime add(OverTime overTime) {
        this.minute = carryBit(this.minute + overTime.minute);
        this.hour = this.hour + overTime.hour;
        return this;
    }

    public OverTime subtract(OverTime overTime) {
        this.minute = carryBit(this.minute - overTime.minute);
        this.hour = this.hour - overTime.hour;
        return this;
    }

    public TYPE getType() {
        return type;
    }

    public void setType(TYPE type) {
        this.type = type;
    }

    @Override
    public int hashCode() {
        return -1;
    }

    @Override
    public boolean equals(Object obj) {
        OverTime overTime = (OverTime) obj;
        if (this.hour > overTime.hour) {
            return true;
        }
        if (this.hour < overTime.hour) {
            return false;
        } else if (this.minute > overTime.minute) {
            return true;
        } else if (this.minute < overTime.minute) {
            return false;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String symbol = "";
        switch (type) {
            case POSITIVE:
                symbol = "";
                break;
            case NEGATIVE:
                symbol = "- ";
                break;
            default:
        }
        return symbol + hour + ":" + minute;
    }
}
